package com.sljy.hzk.ui.task;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sljy.hzk.R;
import com.sljy.hzk.animation.AnimationCallType;
import com.sljy.hzk.animation.AnimationManager;
import com.sljy.hzk.annotation.ActivityInfo;
import com.sljy.hzk.base.BaseActivity;
import com.sljy.hzk.base.UserInfo;
import com.sljy.hzk.entity.MainNoticeNumEntity;
import com.sljy.hzk.entity.TaskBodyEntity;
import com.sljy.hzk.entity.TaskCountEntity;
import com.sljy.hzk.entity.TaskDatabaseEntity;
import com.sljy.hzk.entity.UserEntity;
import com.sljy.hzk.net.BaseCallBack;
import com.sljy.hzk.net.HttpHeper;
import com.sljy.hzk.net.service.UserServive;
import com.sljy.hzk.room.AppDataBase;
import com.sljy.hzk.room.dao.TaskBodyDao;
import com.sljy.hzk.room.dao.TaskDao;
import com.sljy.hzk.ui.QrCodeActivity;
import com.sljy.hzk.util.EventListenerManager;
import com.sljy.hzk.util.NetWorkUtils;
import com.sljy.hzk.util.TimeUtils;
import com.sljy.hzk.view.FocusEffct;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.List;

import butterknife.BindView;

@ActivityInfo(layout = R.layout.activity_task_loading, isShowActionBar = true,title = "我的任务", rightIcRes = R.drawable.homepage)
public class Task_LodingActivity  extends BaseActivity {
    @BindView(R.id.unfinishNum)
    TextView unfinishNum;
    @BindView(R.id.overdueNum)
    TextView overdueNum;
    @BindView(R.id.nouploadNum)
    TextView nouploadNum;
    @BindView(R.id.effct1)
    ImageView effct1;
    @BindView(R.id.effct2)
    ImageView effct2;
    @BindView(R.id.effct3)
    ImageView effct3;
    @BindView(R.id.effct4)
    ImageView effct4;
    private View curView;
    private Integer curBtn;
    TaskDao roomData;
    RelativeLayout efocus1;
    RelativeLayout efocus2;
    RelativeLayout efocus3;
    RelativeLayout efocus4;
    @Override
    protected void init() {
        roomData = AppDataBase.getInstance().getTaskDao();
        if(!NetState())
            return;
        getTaskNumList();
        efocus1 =  findViewById(R.id.focusEffct1);
        efocus2 =  findViewById(R.id.focusEffct2);
        efocus3 =  findViewById(R.id.focusEffct3);
        efocus4 =  findViewById(R.id.focusEffct4);
        onTouchEvent(efocus1);
        onTouchEvent(efocus2);
        onTouchEvent(efocus3);
        onTouchEvent(efocus4);
    }


    /**
     * 任务数量监控
     * */
    private void getTaskNumList(){
        //TODO 请求获取任务今日未完成数量
        if(!NetState()){
            return;
        }
        UserEntity user = UserInfo.getUserInfo();
        HttpHeper.get().create(UserServive.class)
                .getTaskCountDate(user.getCpId(), user.getPId(),TimeUtils.dateToStr(TimeUtils.getNowDate()))
                .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
                .compose(getThread())
                .subscribe(new BaseCallBack<TaskCountEntity>() {
                    @Override
                    public void onCallBackSuccess(TaskCountEntity data) {
                        showState(data.getNotComplateCount(), data.getOverdueCount());
                    }
                    @Override
                    public void onFail(String mes) {
                        showToast(mes);
                    }
                });
    }

    /**
     * 更新显示的数量
     * */
    private void showState(int nfnum, int fnum){
        String date = TimeUtils.dateToStr(TimeUtils.getNowDate());
        int n = 0;
        if(roomData != null){
            List<TaskDatabaseEntity> list = roomData.getUserAll(UserInfo.getUserInfo().getUserId());
            n = list.size();
        }
        unfinishNum.setText((nfnum - n)+"");
        overdueNum.setText(fnum+"");
        nouploadNum.setText(n+"");
    }

    private void onTouchEvent(RelativeLayout view){
        view.setOnClickListener( new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                curBtn = v.getId();
                parentEffect();
            }
        });
    }

    /**
     * 点击效果
     * */
    public void parentEffect(){
        Intent intent = new Intent();
        intent.putExtra("type", curBtn);
        intent.setClass(this,TaskMainActivity.class);
        startActivity(intent);
    }


    //注册事件
    @Override
    protected void registEvent(){
    }
    //移除事件
    @Override
    protected void removeEvent(){
    }

}
